﻿using System;
using RawSocketDll;

namespace WeaponEvalue.Logic
{
    public class DataHandler
    {
        private readonly GameMonitor gameMonitor;
        private readonly RawSocket.PacketArrivedEventArgs packetEvent;

        public DataHandler(GameMonitor gameMonitor, RawSocket.PacketArrivedEventArgs packetEvent)
        {
            this.gameMonitor = gameMonitor;
            this.packetEvent = packetEvent;

        }

        public int MultiByteToInt(byte[] source, int pos)
        {
            return pos < 0 ? 0 : BitConverter.ToInt32(source, pos);
        }

        private static int FindTarget(byte[] p, int startIndex, byte[] sig)
        {
            var index = -1;
            if (startIndex >= p.Length) return index;
            for (var i = startIndex; i < p.Length; i++)
            {
                var isTarget = true;
                for (var j = 0; j < sig.Length; j++)
                {
                    if (i + j >= p.Length)
                    {
                        isTarget = false;
                        break;
                    }
                    if (p[i + j] == sig[j]) continue;
                    isTarget = false;
                    break;
                }
                if (!isTarget)
                {
                    continue;
                }
                index = i;
                break;
            }
            return index;
        }

        public void Execute(object state)
        {
            gameMonitor.GamePacketCount++;
            //gameMonitor.UpdatePacketCount();

            var index = FindTarget(packetEvent.ReceiveBuffer, 0, gameMonitor.NpcNameBytes);

            while (index >= 0)
            {
                var x = MultiByteToInt(packetEvent.ReceiveBuffer, index - 22);
                var y = MultiByteToInt(packetEvent.ReceiveBuffer, index - 18);
                var z = MultiByteToInt(packetEvent.ReceiveBuffer, index - 14);

                LogHelper.GetLogger().AppendLog(gameMonitor, x, y, z);

                if (gameMonitor.PlayMusic) MusicPlayer.PlayMusic();

                if (gameMonitor.AutoOperation && (DateTime.Now - gameMonitor.LastOccurTime).TotalSeconds > 5)
                {
                    var autoPickItem = new AutoPickItem(gameMonitor,x,y,z);
                    autoPickItem.Execute(null);
                }
                gameMonitor.LastOccurTime = DateTime.Now;

                if (index + gameMonitor.NpcNameBytes.Length < packetEvent.ReceiveBuffer.Length)
                {
                    index = FindTarget(packetEvent.ReceiveBuffer, index + gameMonitor.NpcNameBytes.Length, gameMonitor.NpcNameBytes);
                }
                else
                {
                    break;
                }
            }

            index = FindTarget(packetEvent.ReceiveBuffer, 0, new byte[] {0xe0, 0x0b});
            if(index>=0)
            {
                var x = MultiByteToInt(packetEvent.ReceiveBuffer, index +10);
                var y = MultiByteToInt(packetEvent.ReceiveBuffer, index +14);
                var z = MultiByteToInt(packetEvent.ReceiveBuffer, index +18);

                Console.WriteLine("{0},{1},{2}",x,y,z);
                gameMonitor.UpdateLocation(x,y,z);
            }
        }
    }
}
